package com.zilla.android.zillacore.libzilla.lifecircle;

import android.content.Intent;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

/**
 * 生命周期管理器，其自身也实现了生命周期接口
 * Created by chenze on 13-12-2.
 */
public class LifeCircleManager implements ILifeCircle{

    /**
     * 父容器(Activity/Fragment)
     */
    private ILifeCircle container;

    public LifeCircleManager(ILifeCircle container){
        this.container = container;
    }

    private List<ILifeCircle> callbacks = new ArrayList<ILifeCircle>();

    @Override
    public void onCreate(Bundle savedInstanceState){
        for (ILifeCircle callback : callbacks) {
            callback.onCreate(savedInstanceState);
        }
    }

    @Override
    public void onResume(){
        for (ILifeCircle callback : callbacks) {
            callback.onResume();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        for (ILifeCircle callback : callbacks) {
            callback.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onPause(){
        for (ILifeCircle callback : callbacks) {
            callback.onPause();
        }
    }

    @Override
    public void onDestroy() {
        for (ILifeCircle callback : callbacks) {
            callback.onDestroy();
        }
    }

    /**
     * 添加回调
     *
     * @param callback
     */
    public void addCallback(ILifeCircle callback) {
        callbacks.add(callback);
    }
}
